#!/usr/bin/env python
# coding=utf-8
# made by haven200
# 22:30 24-01-2016

import RPi.GPIO as GPIO 
import os
import signal
import sys
import time
from basic import show_message
from basic import write_pid
from basic import remove_pid

LED_PIN=15
BUTTON_PIN=13
shutdown_button_log="/var/log/shutdown_button.log"
shutdown_button_pid="/var/run/shutdown_button.pid"
hcsr_pid="/var/run/hcsr.pid"
hcsr_running=False
press_time=0
count_down=6


def reset_button_pressed_value():
    global press_time, count_down
    press_time=0
    count_down=6

def init():
    global hcsr_running

    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(LED_PIN, GPIO.OUT, initial=GPIO.LOW)
   
    add_button_event_detect()
    signal.signal(signal.SIGTERM, handleSIGTERM)

    if os.path.isfile(hcsr_pid):
        hcsr_running=True
    write_pid(shutdown_button_pid)

def readd_button_event_detect():
    GPIO.remove_event_detect(BUTTON_PIN)
    add_button_event_detect()

def add_button_event_detect():
    GPIO.add_event_detect(BUTTON_PIN, GPIO.FALLING, callback=button_onPress, bouncetime=500)

def handleSIGTERM(signum, frame):
    #cleanup()
    sys.exit(0)

def cleanup():
    GPIO.cleanup()
    remove_pid(shutdown_button_pid)

def flashing_LED():
    GPIO.output(LED_PIN, GPIO.HIGH)
    time.sleep(0.2)
    GPIO.output(LED_PIN, GPIO.LOW)

def button_onPress(channel):
    global press_time, count_down
    press_time+=1
    flashing_LED()
    if press_time > 3:
        show_message("button is cancel", shutdown_button_log)
        reset_button_pressed_value()
    else:
        show_message("button is pressed!", shutdown_button_log)

def start_hcsr():
    os.system("/home/etc/init.d/hcsr start")
    readd_button_event_detect()
    
def stop_hcsr():
    os.system("/home/etc/init.d/hcsr stop")
    readd_button_event_detect()

def restart_computer():
    os.system("shutdown -r now")
    readd_button_event_detect()

def close_computer():
    os.system("shutdown -h now")
    readd_button_event_detect()
    
def button_detect():
    global press_time, count_down, hcsr_running

    try:
        while True:
            if press_time == 1 and count_down == 0:
                if hcsr_running:
                    show_message("stoping hcsr service", shutdown_button_log)
                    hcsr_running=False
                    stop_hcsr()
                else:
                    show_message("starting hcsr service", shutdown_button_log)
                    hcsr_running=True
                    start_hcsr()
                reset_button_pressed_value()
            elif press_time == 2 and count_down == 0:
                show_message("starting shutdown!", shutdown_button_log)
                close_computer()
                reset_button_pressed_value()
            elif press_time == 3 and count_down == 0:
                show_message("restarting computer", shutdown_button_log)
                restart_computer()
                reset_button_pressed_value()
            if press_time > 0 and press_time < 4:
                count_down-=1
                show_message("%s second"%(count_down))
            time.sleep(1)
    except KeyboardInterrupt:
        show_message("user press Ctrl+c, exit!", shutdown_button_log)
    finally:
        cleanup()
        show_message("shutdown_button.py exit!", shutdown_button_log)

if __name__ == "__main__":
    init()
    button_detect()
